<template>
  <div style="width: 50%;border: 1px solid;">
    <div class="card" style="padding:25px 30px;background: linear-gradient(to bottom, #f5f3f2, #92bdce)">
      <div style="font-size: 20px;color: red;display: flex;align-items: center;justify-content: center">
        <span style="margin-bottom: 20px">个人资料修改</span>
      </div>
      <el-form :model="data.form" label-width="100px" style="padding-right: 50px">
        <el-form-item label="账号">
          <el-input :prefix-icon="User" v-model="data.form.username" autocomplete="off" disabled />
        </el-form-item>
        <el-form-item label="真实姓名">
          <el-input :prefix-icon="UserFilled" v-model="data.form.realname" autocomplete="off" disabled />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input :prefix-icon="PhoneFilled" v-model="data.form.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input :prefix-icon="Message" v-model="data.form.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="创新创业学分">
          <el-input :prefix-icon="TrophyBase" v-model="data.form.score" autocomplete="off" disabled />
        </el-form-item>
        <el-form-item label="所属班级" prop="collegeName">
          <el-select style="width: 100%" v-model="data.form.className" disabled>
            <template #prefix>
              <el-icon>
                <HomeFilled />
              </el-icon>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="所属年级" prop="gradeName">
          <el-select style="width: 100%" v-model="data.form.gradeName" disabled>
            <template #prefix>
              <el-icon>
                <CollectionTag />
              </el-icon>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="所属专业" prop="specialityName">
          <el-select style="width: 100%" v-model="data.form.specialityName" disabled>
            <template #prefix>
              <el-icon>
                <CollectionTag />
              </el-icon>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="所属学院" prop="collegeName">
          <el-select style="width: 100%" v-model="data.form.collegeId" disabled>
            <template #prefix>
              <el-icon>
                <School />
              </el-icon>
            </template>
            <el-option v-for="item in data.collegeList" :key="item.collegeId" :label="item.name"
              :value="item.collegeId" />
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select style="width: 100%" v-model="data.form.role" disabled>
            <template #prefix>
              <el-icon>
                <Stamp />
              </el-icon>
            </template>
            <el-option value="SUPER_ADMIN" label="系统管理员"></el-option>
            <el-option value="ADMIN" label="院管理员"></el-option>
            <el-option value="TEACHER" label="教师管理员"></el-option>
            <el-option value="STUDENT" label="学生"></el-option>
          </el-select>
        </el-form-item>
        <div style="text-align: center">
          <el-button style="padding: 10px 25px" type="primary" @click="save">保存个人资料</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { onMounted, reactive } from "vue"
import { ElMessage } from "element-plus";
import alovaInstance from "@/utils/request";
import { School, Stamp, TrophyBase, User, UserFilled } from "@element-plus/icons-vue";

const data = reactive({
  user: JSON.parse(localStorage.getItem('credit-system-user') || '{}'),
  collegeList: [],//学院
  gradeList: [], //年级
  specialityList: [], // 专业
  classesList: [],  //班级
  form: {
    username: '',
    realname: '',
    score: '',
    updateBy: ''
  },
  tableData: [],
  total: ''
})


const emit = defineEmits(["updateUser"])
// 把当前修改的用户信息存储到后台数据库
const save = () => {
  data.form.updateBy = data.user.username
  alovaInstance.Put('/student/collegeAdminStudent/edit', data.form).then(res => {
    if (res.code === '200') {
      ElMessage.success('更新成功')
      //把更新后的用户信息存储到缓存
      Object.assign(data.user, data.form)
      localStorage.setItem('credit-system-user', JSON.stringify(data.user))
      emit('updateUser')
    } else {
      ElMessage.error(res.msg)
    }
  })
}

const loadCollegeList = () => {
  alovaInstance.Get('/sysAdminCollege/selectAll').then(res => {
    if (res.code === '200') {
      data.collegeList = res.data
    }
  })
}

const load = () => {
  alovaInstance.Get('/student/collegeAdminStudent/list', {
    params: {
      stuId: data.user.stuId
    }
  }).then(res => {
    data.form = res.data[0]
    data.total = res.data?.total
    // console.log("==========", data.form.username)
    // console.log(JSON.stringify(data.form, null, 2))
  }).catch(error => {
    console.error('请求失败:', error);
    ElMessage.error(error.message);
  });
}
load()

onMounted(() => {
  loadCollegeList();
  load()
})

</script>

<style scoped>
.avatar-uploader .avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  text-align: center;
}
</style>